﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;

namespace LDSP.BRL
{
    public partial class Enterprise : BYFW.BOL.BaseConfigEntity
    {
        public Enterprise()
        {
            m_tableName = "t_Enterprise";
            m_hasCode = true;
            m_hasComment = true;
            m_tblCacheManager.IsEnable = true;
            m_objCacheManager.Size = 1000;
        }

        protected int m_totalDownloadTimes = 0;
        protected int m_downloadTimes = 0;
        protected string m_address = string.Empty;
        protected int m_industryID = 0;
        protected int m_parentID = 0;
        protected string m_phone = string.Empty;
        protected string m_contact = string.Empty;
        protected string m_email = string.Empty;
        protected string m_module = string.Empty;
        protected bool m_isValid = true;
        protected bool m_isEnterpriseGroup = false;
        protected bool m_supportManageFavorite = false;
        protected bool m_supportSMS = false;

        protected Enterprise m_parent = null;
        protected Industry m_industry = null;
        protected EnterpriseConfig m_enterpriseConfig = null;


        /// <summary>
        /// 总下载次数
        /// </summary>
        public int TotalDownloadTimes
        {
            get { return m_totalDownloadTimes; }
            set { m_totalDownloadTimes = value; }
        }
        /// <summary>
        /// 当前下载次数
        /// </summary>
        public int DownloadTimes
        {
            get { return m_downloadTimes; }
            set { m_downloadTimes = value; }
        }

        /// <summary>
        /// 地址
        /// </summary>
        public string Address
        {
            get { return m_address; }
            set { m_address = value; }
        }
        /// <summary>
        /// 行业ID
        /// </summary>
        public int IndustryID
        {
            get { return m_industryID; }
            set { m_industryID = value; }
        }
        /// <summary>
        /// 电话
        /// </summary>
        public string Phone
        {
            get { return m_phone; }
            set { m_phone = value; }
        }
        /// <summary>
        /// 联系人
        /// </summary>
        public string Contact
        {
            get { return m_contact; }
            set { m_contact = value; }
        }
        /// <summary>
        /// 邮箱
        /// </summary>
        public string Email
        {
            get { return m_email; }
            set { m_email = value; }
        }

        /// <summary>
        /// 授权的模块
        /// </summary>
        public string Module
        {
            get { return m_module; }
            set { m_module = value; }
        }
        /// <summary>
        /// 是否有效
        /// </summary>
        public bool IsValid
        {
            get { return m_isValid; }
            set { m_isValid = value; }
        }

        /// <summary>
        /// 是否集团公司
        /// </summary>
        public bool IsEnterpriseGroup
        {
            get { return m_isEnterpriseGroup; }
            set { m_isEnterpriseGroup = value; }
        }

        /// <summary>
        /// 是否允许企业用户接收短信
        /// </summary>
        public bool SupportSMS
        {
            get { return m_supportSMS; }
            set { m_supportSMS = value; }
        }

        /// <summary>
        /// 是否允许普通用户管理适用法规
        /// </summary>
        public bool SupportManageFavorite
        {
            get { return m_supportManageFavorite; }
            set { m_supportManageFavorite = value; }
        }

        /// <summary>
        /// 母公司ID
        /// </summary>
        public int ParentID
        {
            get { return m_parentID; }
            set { m_parentID = value; }
        }

        /// <summary>
        /// 母公司
        /// </summary>
        public Enterprise Parent
        {
            get
            {
                if (m_parent == null)
                {
                    m_parent = new Enterprise();
                    if (m_parentID != 0)
                    {
                        m_parent.GetProperties(m_parentID);
                    }
                }
                return m_parent;
            }
        }

        public Industry Industry
        {
            get
            {
                if (m_industry == null)
                {
                    m_industry = new Industry();
                    if (m_industryID != 0)
                    {
                        m_industry.GetProperties(m_industryID);
                    }
                }
                return m_industry;
            }
        }

        public EnterpriseConfig EnterpriseConfig
        {
            get
            {
                if (m_enterpriseConfig == null)
                {
                    m_enterpriseConfig = new EnterpriseConfig(m_id);
                }
                return m_enterpriseConfig;
            }
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("totalDownloadTimes", m_totalDownloadTimes);
            m_container.Add("downloadTimes", m_downloadTimes);
            m_container.Add("address", m_address);
            m_container.Add("industryID", m_industryID);
            m_container.Add("phone", m_phone);
            m_container.Add("contact", m_contact);
            m_container.Add("email", m_email);
            m_container.Add("module", m_module);
            m_container.Add("isValid", m_isValid ? 1 : 0);
            m_container.Add("parentID", m_parentID);
            m_container.Add("isGroup", m_isEnterpriseGroup ? 1 : 0);
            m_container.Add("supportSMS", m_supportSMS ? 1 : 0);
            m_container.Add("supportManageFavorite", m_supportManageFavorite ? 1 : 0);
        }

        public override bool GetProperties(System.Data.DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_totalDownloadTimes = this.GetIntValue(record, "totalDownloadTimes");
            m_downloadTimes = this.GetIntValue(record, "downloadTimes");
            m_address = this.GetStringValue(record, "address");
            m_industryID = this.GetIntValue(record, "industryID");
            m_phone = this.GetStringValue(record, "phone");
            m_contact = this.GetStringValue(record, "contact");
            m_email = this.GetStringValue(record, "email");
            m_module = this.GetStringValue(record, "module");
            m_isValid = this.GetBoolValue(record, "isValid");
            m_parentID = this.GetIntValue(record, "parentID");
            m_isEnterpriseGroup = this.GetBoolValue(record, "isGroup");
            m_supportManageFavorite = this.GetBoolValue(record, "supportManageFavorite");
            m_supportSMS = this.GetBoolValue(record, "supportSMS");
            return true;
        }

        public void ResetDownloadTimes()
        {
            m_downloadTimes = 0;
            this.Update();
        }

        public string GetLogoFileFullName()
        {
            try
            {
                return Path.Combine(LDSP.Util.CommonService.SITEROOT, "LogoFiles", m_id + ".png");
            }
            catch { return ""; }
        }

        public void ChangePrincipal(string old, string current)
        {
            IDataParameter param1 = new SqlParameter("@old_principal", old);
            IDataParameter param2 = new SqlParameter("@new_principal", current);
            DB.ExecuteNonQuery("exec change_codification_principal" + " '" + old + "'," + "'" + current + "','" + ID.ToString() + "'");
        }

        /// <summary>
        /// 获取列表形式的模块
        /// </summary>
        /// <returns></returns>
        public List<string> GetModuleList()
        {
            List<string> res = new List<string>();
            if (!string.IsNullOrEmpty(m_module))
            {
                string[] tmp = m_module.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                res.AddRange(tmp);
            }
            return res;
        }

        public List<Enterprise> GetSubsidiaryList()
        {
            return this.GetSpecialList<Enterprise>(null, "parentID", this.m_id);
        }
    }
}
